package com.scada.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class DateCalculation {
	
	public static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	
	/** 
	 * @FunctionName: getMondayOfThisWeek
	 * @Description: 获取本周星期一
	 * @author: 沈浩
	 * @date: 2016年11月28日 下午4:16:19 
	 * @return 
	 */
	public static String getMondayOfThisWeek() {
		Calendar c = Calendar.getInstance();
		int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
		if (day_of_week == 0){
			day_of_week = 7;
		}
		c.add(Calendar.DATE, -day_of_week + 1);
		return df.format(c.getTime());
	}
	
	public static String getMondayOfThisWeek(Calendar c) {
		int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
		if (day_of_week == 0){
			day_of_week = 7;
		}
		c.add(Calendar.DATE, -day_of_week + 1);
		return df.format(c.getTime());
	}
	
	/** 
	 * @FunctionName: getSundayOfThisWeek
	 * @Description: 获取本周星期天
	 * @author: 沈浩
	 * @date: 2016年11月28日 下午4:16:35 
	 * @return 
	 */
	public static String getSundayOfThisWeek() {
		Calendar c = Calendar.getInstance();
		int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
		if (day_of_week == 0){
			day_of_week = 7;
		}
		c.add(Calendar.DATE, -day_of_week + 7);
		return df.format(c.getTime());
	}
	
	public static String getSundayOfThisWeek(Calendar c) {
		int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
		if (day_of_week == 0){
			day_of_week = 7;
		}
		c.add(Calendar.DATE, -day_of_week + 7);
		return df.format(c.getTime());
	}
	
	public static String getThisQuarterStart() {
		Calendar c = Calendar.getInstance();
		String value = String.valueOf(c.get(Calendar.YEAR));
		
		Integer month = c.get(Calendar.MONTH)+1;
		if(month >= 1 && month <= 3){
			value += "-01";
		}else if(month >= 4 && month <= 6){
			value += "-04";
		}else if(month >= 7 && month <= 9){
			value += "-07";
		}else{
			value += "-10";
		}
		
		return value;
	}
	
	public static String getThisQuarterEnd() {
		Calendar c = Calendar.getInstance();
		String value = String.valueOf(c.get(Calendar.YEAR));
		
		Integer month = c.get(Calendar.MONTH)+1;
		if(month >= 1 && month <= 3){
			value += "-03";
		}else if(month >= 4 && month <= 6){
			value += "-06";
		}else if(month >= 7 && month <= 9){
			value += "-07";
		}else{
			value += "-12";
		}
		
		return value;
	}

	public static String getThisQuarterStartLast() {
		Calendar c = Calendar.getInstance();
		String value = String.valueOf(c.get(Calendar.YEAR));
//		c.add(Calendar.MONTH, -1);
		Integer month = c.get(Calendar.MONTH);
		if(month >= 1 && month <= 3){
			value += "-01";
		}else if(month >= 4 && month <= 6){
			value += "-04";
		}else if(month >= 7 && month <= 9){
			value += "-07";
		}else{
			value += "-10";
		}
		
		return value;
	}
	
	public static String getThisQuarterEndLast() {
		Calendar c = Calendar.getInstance();
		String value = String.valueOf(c.get(Calendar.YEAR));
//		c.add(Calendar.MONTH, -1);
		Integer month = c.get(Calendar.MONTH);
		if(month >= 1 && month <= 3){
			value += "-03";
		}else if(month >= 4 && month <= 6){
			value += "-06";
		}else if(month >= 7 && month <= 9){
			value += "-07";
		}else{
			value += "-12";
		}
		
		return value;
	}
}
